import {createRouter, createWebHistory, RouterOptions} from "vue-router";
import {commonRoutes} from "@/router/common";
import {storeToRefs} from "pinia";
import {useUserStore} from "@/store";


const routes = [
    {
        path: '/',
        name: '/',
        children: [
            ...commonRoutes,

        ],
        redirect: '/home'
    },

]

const routerOptions = {
    history: createWebHistory('/redis'),
    routes: routes,
}


const router = createRouter(<RouterOptions>routerOptions)


export default router

router.beforeResolve((to, _, next) => {
    const {userInfo} = storeToRefs(useUserStore())
    if (to.meta.requireLogin) {
        if (userInfo.value.username) {
            next()
        } else {
            next('/auth')
            window.$message.warning('请先登录')
        }
    } else {
        next()
    }
})